* the script to extract ownership links from JSCReg and SReg

* keep JSCReg entries with corresponding posting date
use "JSCReg.dta", clear
keep if D_POST == $maxdate
save "JSCReg_working.dta", replace

* keep SReg entries up until corresponding posting date
use SReg.dta, clear
gen date1 = date(correction_date,"DMY")
replace date1 = date(fund_date, "DMY") if missing(date1)
* 14244 - Jan 1, 1999; 11535 - Aug 1, 1991
keep if date1 <= $maxdate & date1 > 14244
drop date1
save "SReg_working.dta", replace

* check whether firms are present in SReg and JSCReg
* load the main set of oligarch-controlled firms
use Delo_UP.dta, clear
keep okpo

* merge with JSCReg, keep matched firms
merge 1:m okpo using JSCReg_working.dta
keep if _merge==3
contract okpo
drop _freq

* set presence in JSCReg to 1 if found in JSCReg, 0 otherwise
gen present_JSCReg = 1
merge 1:1 okpo using Delo_UP.dta
replace present_JSCReg = 0 if missing(present_JSCReg)
keep okpo present_JSCReg
save "Ownership Data/Delo_UP_present_JSCReg_${filedate}", replace

* load the main set of oligarch-controlled firms
use Delo_UP.dta, clear
keep okpo

* merge with SReg, keep matched firms
merge 1:m okpo using SReg_working.dta
keep if _merge==3
contract okpo
drop _freq

* set presence in SReg to 1 if found in SReg, 0 otherwise
gen present_SReg = 1
merge 1:1 okpo using Delo_UP.dta
replace present_SReg = 0 if missing(present_SReg)
keep okpo present_SReg
save "Ownership Data/Delo_UP_present_SReg_${filedate}", replace

* join two presence measures
merge 1:1 okpo using "Ownership Data/Delo_UP_present_JSCReg_${filedate}"
drop _merge
gen present_JSCReg_SReg = 1 if present_JSCReg == 1 | present_SReg == 1
replace present_JSCReg_SReg = 0 if missing(present_JSCReg_SReg)
save "Ownership Data/Delo_UP_present_JSCReg_SReg_${filedate}", replace

* main owner search
* step 0: prepare the list of oligarch-controlled firms
use Delo_UP.dta, clear
gen double Delo_UP_firm = okpo
gen country = 804
gen entity = 1
rename okpo id
gen step = 0
order name entity country, after(id)
save all_entities.dta, replace

* prepare the okpo (identifier) list for JSCReg search
drop step
sort id Delo_UP_firm
quietly by id Delo_UP_firm:  gen dup = cond(_N==1,0,_n) 
drop if dup > 1
drop dup
rename name prev_name
rename entity prev_entity
rename country prev_country
rename id okpo
save for_JSCReg_owner_search.dta, replace

* set variables to iterate on
* iteration number
local currentstep = 1
* the number of firms (identifiers) to check
* if after an iteration, there are no more Ukrainian firms to check, stop the search
local okpoleft = _N

while `okpoleft' > 0 {
* search for owners of oligarch-controlled firms JSCReg
do extract_owners_JSCReg.do

* remove irrelevant owners
do JSCReg_clean.do

* search for owners in SReg
do extract_owners_SReg.do

* remove irrelevant owners
do SReg_clean_corporate.do
do SReg_clean_individual.do

* combine the results from different searches in a data frame for the current iteration
use owners_from_SReg_ind_final.dta, clear
append using owners_from_SReg_clean_corporate.dta
gen source = "SReg"
append using corp_owners_from_JSCReg_clean.dta
replace source = "JSCReg" if missing(source)
gen step = `currentstep'
save `"step_`currentstep'_all"', replace

* check if entities found in current iteration were already found in previous iterations
* if yes, don't search for them anymore
use `"step_`currentstep'_all"', clear
merge m:m id entity country using all_entities.dta
keep if _merge==1
drop _merge prev_id prev_entity prev_country prev_name source step owner

* keep Ukrainian firms (owners) for JSCReg search
keep if entity == 1 & country == 804

* prepare the list of okpos (identifiers) to search for
if _N > 0 {
sort id Delo_UP_firm 
quietly by id Delo_UP_firm:  gen dup = cond(_N==1,0,_n) 
drop if dup > 1
drop dup
rename name prev_name
rename entity prev_entity
rename country prev_country
rename id okpo
}

* save the list of Ukrainian owners to search for their owners
save for_JSCReg_owner_search.dta, replace
local okpoleft = _N

* add owners found in current iteration to those found in previous iterations
use `"step_`currentstep'_all"', clear
append using all_entities.dta
save all_entities.dta, replace

display `currentstep'

local currentstep = `currentstep' + 1
}

* save final data set
save "all_entities_${filedate}", replace

**** prepare data for network analysis
do network_preparation_Delo_UP.do

* erase temporary files
local list : dir . files "step*.dta"
foreach f of local list {
    erase "`f'"
}
local list : dir . files "*owners_from*.dta"
foreach f of local list {
    erase "`f'"
}
local list : dir . files "id_affiliations*.dta"
foreach f of local list {
    erase "`f'"
}
erase for_JSCReg_owner_search.dta
erase for_SReg_owner_search_all_owners.dta
erase for_SReg_owner_search_ind_owners.dta
erase "Ownership Data/Delo_UP_present_JSCReg_${filedate}.dta"
erase "Ownership Data/Delo_UP_present_SReg_${filedate}.dta"
erase all_entities.dta
erase "all_entities_${filedate}.dta"
erase foreign_entities_fixed.dta
